package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseLink;
import com.maoye.ossp.sys.base.service.SysBaseLinkService;
import com.maoye.ossp.sys.org.util.UserUtils;

/**
 * 常用资料Controller
 * @author yanzy
 *
 */
@Controller
@RequestMapping(value = "/sys/base/link")
public class SysBaseLinkController extends BaseController{
	
	@Resource
	private SysBaseLinkService sysBaseLinkService;
	
	/**
	 * 获得所有信息展示
	 * @param sysBaseLink
	 * @return
	 * @data 2014年7月11日
	 */
	@RequestMapping(value = "/list")
	public String list(Model model,HttpServletRequest request,SysBaseLink sysBaseLink){
		QueryFilter queryFilter = getQuerFilter(request,sysBaseLink);
		Page<SysBaseLink> page = (Page<SysBaseLink>)sysBaseLinkService
				.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/sys/base/link/list", model, request);
	}
	
	@RequestMapping(value = "/addLink")
	public String addLink(Model model){
		SysBaseLink sysBaseLink = new SysBaseLink();
		model.addAttribute("sysBaseLink",sysBaseLink);
		return "/sys/base/link/add";
	}
	
	/**
	 * 保存常用资料的信息
	 * @param sysBaseLink
	 * @data 2014年7月11日
	 */
	@RequestMapping(value = "/saveLink")
	public String saveLink(SysBaseLink sysBaseLink,HttpServletRequest request,RedirectAttributes redirectAttributes){
		sysBaseLink.setId(idGenerator.getUId());
		sysBaseLink.setCreateBy(UserUtils.getCurrentUserId().toString());
		sysBaseLink.setUpdateBy("");
		sysBaseLinkService.saveLink(sysBaseLink);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:/sys/base/link/list";
	}
	
	/**
	 * 查看单条记录的详细信息
	 * @param model
	 * @param request
	 * @return
	 * @data 2014年7月11日
	 */
	@RequestMapping(value = "/viewLink")
	public String view(Model model,HttpServletRequest request){
		String id = request.getParameter("id");
		SysBaseLink sysBaseLink = sysBaseLinkService.getLinkById(id);
		model.addAttribute("sysBaseLink",sysBaseLink);
		return resultPage("/sys/base/link/view", model, request);
	}
	
	/**
	 * 删除常用资料信息
	 * @param model
	 * @param request
	 * @param redirectAttributes
	 * @return
	 * @data 2014年7月11日
	 */
	@RequestMapping(value = "/removeLink")
	public String removeLink(Model model,HttpServletRequest request,RedirectAttributes redirectAttributes){
		String id = request.getParameter("id");
		sysBaseLinkService.deleteLink(id);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:/sys/base/link/list";
	}
	
	@RequestMapping(value = "/updateLink")
	public String updateLink(Model model,HttpServletRequest request){
		String id = request.getParameter("id");
		SysBaseLink sysBaseLink = sysBaseLinkService.getLinkById(id);
		model.addAttribute("sysBaseLink", sysBaseLink);
		return "/sys/base/link/update";
	}
	
	/**
	 * 更新常用资料信息
	 * @param sysBaseLink
	 * @param redirectAttributes
	 * @return
	 * @data 2014年7月11日
	 */
	@RequestMapping(value = "/savaUpdateLink")
	public String savaUpdateLink(SysBaseLink sysBaseLink,RedirectAttributes redirectAttributes){
		sysBaseLink.setUpdateBy(UserUtils.getCurrentUserId().toString());
		sysBaseLinkService.saveUpdateLink(sysBaseLink);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:/sys/base/link/list";
	}
	
	@RequestMapping(value = "/getAllLink")
	public void getAllLink(HttpServletRequest request, HttpServletResponse response)  
	            throws ServletException, IOException {    
	    List<SysBaseLink> list = sysBaseLinkService.getAllLink(); 
        PrintWriter out=response.getWriter();  
        JSONArray jsonArray=JSONArray.fromObject(list);  
        out.print(jsonArray);  
        out.flush();  
        out.close();  
	} 
	
	/**
	 * 检查是否含有该排序号
	 * @param request
	 * @param response
	 * @throws IOException
	 * @author Administrator
	 * @data 2014年7月30日
	 */
	@RequestMapping(value = "/checkOrderNo")
	public void checkOrderNo(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		try {
			int orderNo = Integer.parseInt(request.getParameter("orderNo"));
			map.put("orderNo",orderNo);
		} catch (NumberFormatException e) {
			System.out.println("数字格式错误，不处理！");
		}	
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseLinkService.checkOrderNo(map)){
			json.put("orderNo",0); //允许添加新排序号
		}else{
			json.put("orderNo",1);//排序号已存在
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	
	@RequestMapping(value="service")
	public String service(Model model){
		return "/service";
	}
	
	@RequestMapping(value="account")
	public String account(Model model){
		return "/account";
	}
	
}
